//
//  KWMRequestResult.m
//  iLiJiPao
//
//  Created by kevin on 4/27/15.
//  Copyright (c) 2015 kollway. All rights reserved.
//

#import "KWMRequestResult.h"

@implementation KWMRequestResult{

}

- (instancetype)initWithDictionary:(NSDictionary *)dict
                        modelClass:(Class)modelClass
                             error:(NSError **)err {
    self = [super initWithDictionary:dict error:err];
    if (self) {
        NSDictionary *dataDictionary = dict[@"data"];
        if(dataDictionary){
            self.data = [self buildData:modelClass jsonDictionary:dataDictionary];
        }
    }
    
    return self;
}

- (id)buildData:(Class)modelClass jsonDictionary:(NSDictionary *)jsonDictionary {
    id result = nil;
    
    if(modelClass != nil){
        NSError *error;
        id tmpResult = [[modelClass alloc] initWithDictionary:jsonDictionary error:&error];
        if(error){
            NSLog(@"buildData error: %@", [error localizedDescription]);
        }else{
            result = tmpResult;
        }
    }
    
    return result;
}

+ (JSONKeyMapper *)keyMapper {
    return [[JSONKeyMapper alloc] initWithDictionary:@{
                                                       @"code" : @"code",
                                                       @"message" : @"message",
                                                       @"page_size" : @"pageSize"
                                                       }];
}

@end
